package com.lizhijun.service.impl;

import com.lizhijun.mapper.AccountMapper;
import com.lizhijun.pojo.Account;
import com.lizhijun.service.AccountService;
import com.lizhijun.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("accountService")
public class AccountServiceImpl implements AccountService {

    private SqlSessionFactory sqlSessionFactory = MybatisUtil.getSqlSessionFactory();

    @Override
    public void save(Account account) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
        mapper.save(account);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public List<Account> findAll() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
        List<Account> accountList = mapper.findAll();
        sqlSession.close();
        return accountList;
    }
}
